/**
 * Copyright (c) 2015 www.smarthome.com All rights reserved. <br />
 * 本软件源代码版权归xxxx公司,未经许可不得任意复制与传播.<br />
 *
 */
package com.smarthome.core.kuaidiXiyi.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShAccountService;
import com.smarthome.base.service.ShExpressFeeService;
import com.smarthome.base.service.ShHistoryAccountService;
import com.smarthome.base.service.ShOrdersService;
import com.smarthome.core.kuaidiXiyi.SendCourierService;
import com.smarthome.core.messageTask.MessageTaskService;
import com.smarthome.domain.bean.ShExpressFee;
import com.smarthome.domain.bean.ShHistoryAccount;
import com.smarthome.domain.bean.ShOrders;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.domain.vo.boss.TaskMessageVo;

/**
 * 
* @ClassName: SendCourierServiceImpl
* @Description: 发快递service接口实现类
* @author cyj
* @date 2016年3月24日 上午10:50:10
*
 */
@Service("sendCourierService")
public class SendCourierServiceImpl implements SendCourierService  , MessageTaskService{


    @Resource
    private ShOrdersService shOrdersOfSendCourierService;
    
    
    @Resource
    private ShHistoryAccountService shHistoryAccountService;
    
    @Resource
    private ShAccountService shAccountService;

    @Resource
	private ShExpressFeeService shExpressFeeService;
    
    @Override
    public DataGrid dataGrid(ShOrders shOrders, PageHelper page) {
        return shOrdersOfSendCourierService.queryShOrdersPage(shOrders, page);
    }

   /* @Override
    public DataGrid dataOrdGridPage(ShOrders shOrders, PageHelper page){
    	
    }
    */
    
	@Override
	public ShOrders getDetailById(Long id) {
		return shOrdersOfSendCourierService.getDetailById(id);
	}


	@Override
	public Integer add(ShOrders shOrders) {
		Date nowDate = new Date();
		
		
		ShHistoryAccount desHistoryAccount = null;
		if(shOrders.getDesAccountId() != null) {
			desHistoryAccount = shHistoryAccountService.selectEntryById(shOrders.getDesAccountId());
			
		}else {
			desHistoryAccount = new ShHistoryAccount();
			desHistoryAccount.setGmtCreate(nowDate);
			desHistoryAccount.setStatus(1);
		}
		desHistoryAccount.setGmtModify(nowDate);
		desHistoryAccount.setAccountId(shOrders.getSrcAccountId());
		desHistoryAccount.setAreaDetail(shOrders.getDesAreaDetail());
		desHistoryAccount.setAreaId(shOrders.getDesAreaId());
		desHistoryAccount.setMobile(shOrders.getDesMobile());
		desHistoryAccount.setName(shOrders.getDesAccountName());
		//desHistoryAccount.setResidenceId(shOrders.getResidenceId());
		try{
			shHistoryAccountService.saveOrUpdate(desHistoryAccount);
		}catch(Exception e) {
			e.printStackTrace();
		}
		shOrders.setDesAccountId(desHistoryAccount.getId());
		
		
		shOrders.setGmtCreate(nowDate);
		shOrders.setGmtModify(nowDate);
		shOrders.setOrderType(2);
		shOrders.setStatus(1);
		shOrders.setCourierType(1);
		
		int result = 0;
		try{
			result = shOrdersOfSendCourierService.insert(shOrders);
		}catch(Exception e) {
			e.printStackTrace();
			return 0;
		}
		return result;
	}




	@Override
	public Integer edit(ShOrders shOrders) {
		Date nowDate = new Date();
		/*ShOrders orders = shOrdersOfSendCourierService.selectEntryById(shOrders.getId());
		BeanUtils.copyProperties(shOrders, orders);
		orders.setGmtModify(nowDate);
		orders.setCourierType(1);*/
		shOrders.setGmtModify(nowDate);
		shOrders.setCourierType(1);
		
		ShHistoryAccount desHistoryAccount = shHistoryAccountService.selectEntryById(shOrders.getDesAccountId());
		desHistoryAccount.setAccountId(shOrders.getSrcAccountId());
		desHistoryAccount.setAreaDetail(shOrders.getDesAreaDetail());
		desHistoryAccount.setAreaId(shOrders.getDesAreaId());
		desHistoryAccount.setGmtModify(nowDate);
		desHistoryAccount.setMobile(shOrders.getDesMobile());
		desHistoryAccount.setName(shOrders.getDesAccountName());
		try{
			shHistoryAccountService.saveOrUpdate(desHistoryAccount);
		}catch(Exception e) {
			e.printStackTrace();
		}
		

		int result = 0;
		try{
			result = shOrdersOfSendCourierService.saveOrUpdate(shOrders);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return result;
	}




	@Override
	public Integer chengeStatus(Long id,Integer status) {
		ShOrders shOrders = shOrdersOfSendCourierService.selectEntryById(id);
		shOrders.setStatus(status);
		return shOrdersOfSendCourierService.saveOrUpdate(shOrders);
	}




	@Override
	public ShHistoryAccount getHistoryAccountInfo(ShHistoryAccount historyAccount) {
		List<ShHistoryAccount> historyAccounts = shHistoryAccountService.selectEntryList(historyAccount);
		if(historyAccounts != null && !historyAccounts.isEmpty()) {
			historyAccount = historyAccounts.get(0);
		}
		return historyAccount;
	}




	@Override
	public List<ShOrders> getSendCourierDetailList(ShOrders shOrders) {
		return shOrdersOfSendCourierService.getSendCourierDetailList(shOrders);
	}


	@Override
	public TaskMessageVo getTaskMessage(Long residenceId) {
		TaskMessageVo vo = new TaskMessageVo();
		
		vo.setMessageType(TaskMessageVo.TaskMessageType.SENDCOURIER);
		ShOrders shOrders = new ShOrders();
		shOrders.setResidenceId(residenceId);
		shOrders.setStatus(1);
		shOrders.setOrderType(2);
		vo.setCount(shOrdersOfSendCourierService.getSendCourierCount(shOrders).longValue());
		vo.setParam("status=1");
		return vo;
	}

	@Override
	public Long getExpressTotalFee(ShOrders shOrders) {
		if(shOrders.getWeight() == null) {
			return null;
		}
		ShExpressFee expressFee = new ShExpressFee();
		expressFee.setProvinceId(shOrders.getDesProvinceId());
		expressFee.setExpressId(shOrders.getCompanyId());
		expressFee.setStatus(1);
		List<ShExpressFee> list = shExpressFeeService.selectEntryList(expressFee);
		if(list == null || list.isEmpty()) {
			return null;
		}
		expressFee = list.get(0);
		if(expressFee.getFirstWeight().intValue() >= shOrders.getWeight().intValue()) {
			return expressFee.getFirstWeightFee();
		}else{
			return expressFee.getFirstWeightFee() + (shOrders.getWeight().intValue() - expressFee.getFirstWeight().intValue())/1000*expressFee.getAddedWeightFee(); 
		}
	}
   
}
